import http from '@/axios/http'

const newsOperation = {
  //根据id获取公告信息
  selectAll: (params: any) => {
    return http
      .get('/NewsController/findNewsByid', params)
      .then((res) => {
        if (res.code == 200) {
          return res
        } else {
          throw new Error(res.message || 'FIND failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during FIND:', error)
        throw error // 继续抛出错误，让调用方可以捕获
      })
  },

  //获取所有操作员信息
  selectAllCreateName: () => {
    return http
      .get('/NewsController/queryOperNameList')
      .then((res) => {
        if (res.code == 200) {
          return res
        } else {
          throw new Error(res.message || 'FIND failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during FIND:', error)
        throw error // 继续抛出错误，让调用方可以捕获
      })
  },

  //获取所有公告信息
  selectAllT: (params: any) => {
    return http
      .post('/NewsController/findAllNews', params)
      .then((res) => {
        if (res.code == 200) {
          return res
        } else {
          throw new Error(res.message || 'FIND failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during FIND:', error)
        throw error // 继续抛出错误，让调用方可以捕获
      })
  },

  // 根据id删除公告信息
  deletUser: (params: any) => {
    return http
      .delete('/NewsController/deleteNews', params)
      .then((res) => {
        if (res.code == 200) {
          return res.data
        } else {
          // 登录失败，抛出错误以便外部catch捕获
          throw new Error(res.message || 'DELET failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during DELET:', error)
        throw error
      })
  },

  //根据id编辑公告信息
  update: (params: any) => {
    return http
      .put('/NewsController/updateNewsById', params)
      .then((res) => {
        if (res.code == 200) {
          return res.data
        } else {
          // 登录失败，抛出错误以便外部catch捕获
          throw new Error(res.message || 'DELET failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during DELET:', error)
        throw error
      })
  },

  // 新增公告信息
  insert: (params: any) => {
    return http
      .post('/NewsController/insertNews', params)
      .then((res) => {
        if (res.code == 200) {
          return res.data
        } else {
          // 登录失败，抛出错误以便外部catch捕获
          throw new Error(res.message || 'DELET failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during DELET:', error)
        throw error
      })
  },
}

export default newsOperation
